package com.heroCW.bodhiAttic.controller;

import com.heroCW.bodhiAttic.common.BaseContext;
import com.heroCW.bodhiAttic.entity.AddressBook;
import com.heroCW.bodhiAttic.resultUtil.Result;
import com.heroCW.bodhiAttic.resultUtil.ResultCode;
import com.heroCW.bodhiAttic.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 地址簿管理
 */
@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增地址
     *
     * @param addressBook
     * @return
     */
    @PostMapping
    public Result<AddressBook> save(@RequestBody AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        log.info("addressBook:{}", addressBook);
        boolean flag = addressBookService.saveAddressBook(addressBook);

        if (flag) {
            return Result.success(ResultCode.SAVE_OK, addressBook, "地址新增成功");
        } else {
            return Result.error(ResultCode.SAVE_ERR, "地址新增失败");
        }
    }

    /**
     * 根据id删除地址
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result<String> delete(@RequestParam Long ids) {
        log.info("ids: {}", ids);
        boolean flag = addressBookService.removeById(ids);
        if (flag) {
            return Result.success(ResultCode.DELETE_OK, "地址删除成功");
        } else {
            return Result.error(ResultCode.DELETE_ERR, "地址新增失败");
        }
    }

    /**
     * 设置默认地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping("default")
    public Result<AddressBook> setDefault(@RequestBody AddressBook addressBook) {
        log.info("addressBook:{}", addressBook);

        Boolean flag = addressBookService.setDefault(addressBook);

        if (flag) {
            return Result.success(ResultCode.UPDATE_OK, addressBook, "默认地址修改成功");
        } else {
            return Result.error(ResultCode.UPDATE_ERR, "默认地址修改失败");
        }
    }

    /**
     * 修改地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping
    public Result<String> updateAddressBook(@RequestBody AddressBook addressBook) {
        log.info("AddressBook: {}", addressBook);

        boolean flag = addressBookService.updateById(addressBook);

        if (flag) {
            return Result.success(ResultCode.UPDATE_OK, "地址修改成功");
        } else {
            return Result.error(ResultCode.UPDATE_ERR, "地址修改失败");
        }
    }

    /**
     * 根据id查询地址
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<AddressBook> get(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook != null) {
            return Result.success(ResultCode.GET_OK, addressBook);
        } else {
            return Result.error(ResultCode.GET_ERR, "没有找到该对象");
        }
    }

    /**
     * 查询默认地址
     *
     * @return
     */
    @GetMapping("default")
    public Result<AddressBook> getDefault() {

        AddressBook addressBook = addressBookService.getDefault();

        if (addressBook != null) {
            return Result.success(ResultCode.GET_OK, addressBook);
        } else {
            return Result.error(ResultCode.GET_ERR, "尚未设置默认地址");
        }
    }

    /**
     * 查询指定用户的全部地址
     *
     * @param addressBook
     * @return
     */
    @GetMapping("/list")
    public Result<List<AddressBook>> list(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        log.info("addressBook:{}", addressBook);

        List<AddressBook> list = addressBookService.getList(addressBook);

        if (list != null) {
            return Result.success(ResultCode.GET_OK, list);
        } else {
            return Result.error(ResultCode.GET_ERR, "暂无地址信息");
        }
    }
}
